import java.util.PriorityQueue;

class KthLargest {
    // 该题找第k大 应该创建一个小根堆
    PriorityQueue<Integer> heap;
    int _k;

    public KthLargest(int k, int[] nums) {
        _k = k;
        // 初始化为小根堆
        heap = new PriorityQueue<>();
        // 将数据填入堆
        for (int x : nums) {
            heap.offer(x);
            if (heap.size() > _k) {
                heap.poll();
            }
        }
    }

    public int add(int val) {
        heap.offer(val);
        if (heap.size() > _k) {
            heap.poll();
        }
        return heap.peek();
    }

}

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest obj = new KthLargest(k, nums);
 * int param_1 = obj.add(val);
 */